package com.tocea.formation.tpjuni4;

import java.util.Collection;

public class SetImpl implements ISet {

	private Integer[] ensemble;
	
	public SetImpl(Integer[] e){
		this.ensemble = e;
	}
	
	public int size() {
		int number = 0;
		for(Integer i : this.ensemble) {
			if(i != null) number ++;
		}
		return number;
	}

	public boolean isEmpty() {
		return (this.size() == 0) ? true : false;
	}

	public boolean contains(Integer o) {
		if(o == null)
			throw new NullPointerException("Ne peut pas ajouter un entier de valeur nulle");
		
		for(Integer i : this.ensemble){
			if(i.equals(o)) return true;
		}
		
		return false;
	}

	public boolean add(Integer e) {
		if(e == null) {
			throw new NullPointerException("Ne peut pas ajouter un entier de valeur nulle");
		}
		else if(this.contains(e)) {
			throw new UnsupportedOperationException("Valeur déjà comprise dans le tableau");
		}
		else if(this.size() != this.ensemble.length) {
			throw new UnsupportedOperationException("Tableau complet");
		}
		else {
			this.ensemble[this.size() + 1] = e;
			return true;
		}
	}

	public boolean remove(Object o) {
		int number = 0;
		for(Integer i : this.ensemble){
			if(i.equals(o)) {
				this.ensemble[number] = null;
				return true;
			}
			number ++;
		}
		return false;
	}

	public boolean addAll(Collection<Integer> c) {
		int number = 1;
		for(Integer i : c)
		{
			this.ensemble[this.size() + number] = i;
			number ++;
		}
		return true;
	}

	public boolean removeAll(Collection<Integer> c) {
		int number = 0;
		for(Integer i : this.ensemble) {
			for(Integer j : c) {
				if(i.equals(j)) this.ensemble[number] = null;
			}
			number ++;
		}
		return false;
	}

	public void clear() {
		this.ensemble = null;
	}

}
